package com.sinodata.bsm.center.web.action.resource.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.sinodata.bsm.center.cache.ResRelationCache;

import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UserFunctionValidate;
import com.sinodata.bsm.center.user.UserUtil;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.vo.ResRelation;

public class NodePrivilegeUtil {
    /**
     * 缓存当前用户的资源权限
     * @param request
     * @return
     */
    public static Map<Long, Boolean> getUserPrivilegeMap(HttpServletRequest request) {
        Map<Long, Boolean> map = (Map<Long, Boolean>) request.getSession().getAttribute("UserPrivilegeMap");
        if (map == null) {
            map = new HashMap<Long, Boolean>(1000);
            List<Long> resList = UserFunctionValidate.getResIdListForUser(FunctionConstants.VIEW_RES);
            for (Long resId : resList) {
                map.put(resId, true);
            }
        }
        return map;
    }

    private static ResRelationCache resRelationCache = SpringContextHolder.getBean(ResRelationCache.class);

    /**
     * 判断给出的resId是否有查看资源权限
     * @param resId
     * @param validCache
     * @param relationTypesIds
     * @return
     */
    public static boolean hasResViewPrivilege(long resId, Set<Long> relationTypesIds, HttpServletRequest request) {
        Boolean isSysAdmin = (Boolean) request.getSession().getAttribute("UserIsGuest");
        if (isSysAdmin == null) {
            if (UserUtil.getInstance().isSysAdmin(UserUtil.getInstance().getCurrentUserId()) || UserUtil.getInstance().isGuest(UserUtil.getInstance().getCurrentUserId())) {
                request.getSession().setAttribute("UserIsGuest", true);
                return true;
            } else {
                request.getSession().setAttribute("UserIsGuest", false);
            }
        } else {
            if (isSysAdmin.booleanValue()) {
                return true;
            }
        }
        Map<Long, Boolean> validCache = getUserPrivilegeMap(request);
        if (validCache.containsKey(resId)) {
            return validCache.get(resId);
        }
        List<ResRelation> relations = resRelationCache.getResRelationsOfRes(resId);
        if (relations != null) {
            for (ResRelation resRelation : relations) {
                if (resRelation.getEndId().longValue() == resId && relationTypesIds.contains(resRelation.getRelationTypeId().longValue())) {
                    long pResId = resRelation.getStartId();
                    if (validCache.containsKey(pResId)) {
                        if (validCache.get(pResId)) {
                            return true;
                        }
                    } else {
                        boolean bln = hasResViewPrivilege(pResId, relationTypesIds, request);
                        validCache.put(pResId, bln);
                        if (bln) {
                            return true;
                        }
                    }
                }
            }
        }
        validCache.put(resId, false);
        return false;
    }

    /**
     * 获取当前session的树节点的缓存对象
     * @param request
     * @param viewId
     * @return
     */
    public static Map<String, Integer[]> getStatusCache(HttpServletRequest request, Long viewId) {
        Map<String, Integer[]> statusCache = (Map<String, Integer[]>) request.getSession().getAttribute("viewTree#" + viewId);
        if (statusCache == null) {
            statusCache = new HashMap<String, Integer[]>(300);
            request.getSession().setAttribute("viewTree#" + viewId, statusCache);
        }
        return statusCache;
    }

    /**
     * 获取当前session的上一次点击树的轨迹
     * @param request
     * @param viewId
     * @return
     */
    public static List<String> findResNodeTrace(HttpServletRequest request, Long viewId) {
        Map<String, List<String>> map = (Map<String, List<String>>) request.getSession().getAttribute("nodeTrace");
        if (map == null) {
            map = new HashMap<String, List<String>>();
            request.getSession().setAttribute("nodeTrace", map);
        }
        List<String> nodeTrace = map.get(String.valueOf(viewId.longValue()));
        return nodeTrace == null ? new ArrayList<String>(0) : nodeTrace;
    }
}
